<?php
/**
 * 价格管理
 * User: hls
 * Date: 2016/3/21
 * Time: 10:10
 */

namespace Admin\Controller;
use Admin\BaseController;
use Common\Model\BusinessModel;
use Common\Model\RoomModel;
use Common\Model\UserModel;
use Common\Model\UtilsModel;
use Think\Controller;
use Think\Db;
use Think\Page;

class PriceController extends BaseController
{

    /**
     * 门店列表
     */
    public function priceList(){
        if(IS_POST){

            $timeSTMP=i('post.time',0,'intval');
            if($timeSTMP ==0) {
                $today = date('Y-m-d');
                $timeSTMP = strtotime($today);
            }



            $weekarray_show=array("日","一","二","三","四","五","六");

            $w = date("w",$timeSTMP);

            if($w!=0){
                $startTime = strtotime(0-$w." days",$timeSTMP);
            }
            else{
                $startTime = $timeSTMP;
            }
            $startDate=date('Y-m-d',$startTime);

            ///////////////////////////////////////////////////
            $endDate=date('Y-m-d',$startTime+UtilsModel::ONE_DAY * 13);

            $list=M('r_room',null,DB_MAIN_CFG)->getField('rid,roomName,status');


            $rooms=M('r_price_common',null,DB_MAIN_CFG)->select();

            foreach($rooms as $room){
                $list[$room['rid']]['workDayPrice']=$room['workDayPrice'];
                $list[$room['rid']]['weekEndPrice']=$room['weekEndPrice'];
                $list[$room['rid']]['devide']=$room['devide'];
                $list[$room['rid']]['workDayPrice2']=$room['workDayPrice2'];
                $list[$room['rid']]['weekEndPrice2']=$room['weekEndPrice2'];
                $list[$room['rid']]['data']=array();
            }

            $startTmp   = strtotime($startDate);
            $endTmp     = strtotime($endDate);


            $dateArr=array();
            for($t=$startTmp;$t<=$endTmp;$t+=UtilsModel::ONE_DAY){
                $tmpDate=date('Y-m-d',$t);
                $dateArr[]=$tmpDate;
                $w=date('w',$t);

                switch ($w){
                    case 0:
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                        $isWorkDay=true;
                        break;
                    case 5:
                    case 6:
                        $isWorkDay=false;
                        break;
                }
                foreach ($list as $rid=>$room){
                    if($room['devide'] !='0000-00-00' && $tmpDate >= $room['devide']){
                        if ($isWorkDay){
                            $v=$room['workDayPrice2'];
                        }else{
                            $v=array('e',$room['weekEndPrice2']);
                        }
                    }else{
                        if ($isWorkDay){
                            $v=$room['workDayPrice'];
                        }else{
                            $v=array('e',$room['weekEndPrice']);
                        }


                    }
                    if(array_key_exists('data',$list[$rid])) {
                        $list[$rid]['data'][$tmpDate] = $v;
                    }
                }

            }

            $specials=M('r_price_special',null,DB_MAIN_CFG)->where("date between '{$startDate}' and '{$endDate}'")->order('rid,date')->select();
            foreach ( $specials as $special){
                if(array_key_exists('data',$list[$rid])) {
                    $list[$special['rid']]['data'][$special['date']] = array('s', $special['price']);
                }
            }


            $this->smarty->assign('week',$weekarray_show);
            $this->smarty->assign('dateArr',$dateArr);
            $this->smarty->assign('list',$list);
            $html=$this->smarty->fetch('Price/price_list_do.html');
            $this->ajaxReturn(array('status'=>0,'HTML'=>$html,'time'=>$startTime));
        }
        else{
            $this->smarty->display('Price/price_list.html');
        }


    }


    public function setSpecial(){
        $rid=I('post.rid',0,'intval');
        $startDate=I('post.startDate');
        $endDate=I('post.endDate');
        $price=I('post.price',0,'intval');
        if($price<=0){
            $this->ajaxReturn(array('status'=>1,'msg'=>'房价必须大于0'));
        }
        if($rid<=0){
            $this->ajaxReturn(array('status'=>1,'msg'=>'请指定房间'));
        }

        $startTime  = strtotime($startDate);
        $endTime    = strtotime($endDate);
        if($startTime==0 || $endTime==0){
            $this->ajaxReturn(array('status'=>1,'msg'=>'日期范围错误'));
        }

        $data=array();
        for($tmp=$startTime;$tmp<=$endTime;$tmp+=UtilsModel::ONE_DAY){
            $data[]=array(
                'rid'=>$rid,
                'date'=>date('Y-m-d',$tmp),
                'price'=>$price
            );
        }
        if (count($data)>0){
            M('r_price_special',null,DB_MAIN_CFG)->addAll($data,null,true);
        }
        $this->ajaxReturn(array('status'=>0,'msg'=>'success'));
    }

    public function setCommon(){

        $rules = array(
            array('rid', 'require', '房间必须指定'),
        );
        $priceCommon=M('r_price_common',null,DB_MAIN_CFG);
        $data=$priceCommon->validate($rules)->create();
        if ($data===false){
            $this->ajaxReturn(array('status'=>1,'msg'=>$priceCommon->getError()));
        }
        $ret=$priceCommon->add($data,null,true);
        if ($ret===false){
            $this->ajaxReturn(array('status'=>1,'msg'=>$priceCommon->getError()));
        }else{
            $this->ajaxReturn(array('status'=>0,'msg'=>'success'));
        }

    }
}
